package com.study.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.study.service.LoginService;

import java.util.Map;

public class LoginAction extends ActionSupport {
    private Login login;
    private LoginService loginservice;

    public Login getLogin() {
        return login;
    }

    public void setLogin(Login login) {
        this.login = login;
    }

    public LoginService getLoginservice() {
        return loginservice;
    }

    public void setLoginservice(LoginService loginservice) {
        this.loginservice = loginservice;
    }

    public String execute() throws Exception {
        Login l = loginservice.checkLogin(login.getName(), login.getPassword());
        if (l != null) {
            Map session = (Map) ActionContext.getContext().getSession();
            session.put("login", l);
        }
        return SUCCESS;
    }

    //验证用户名和密码不为空
    @Override
    public void validate() {
        if (login.getName() == null || login.getName().equals("")) {
            this.addFieldError("name", "用户名不能为空！");
        } else if (login.getPassword() == null || login.getPassword().equals("")) {
            this.addFieldError("password", "密码不能为空！");
        }
    }
}

